<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Snake in IronPython Mono Winforms</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Mono, IronPython, Winforms, games, tutorial, 
    Snake, programming, GUI, multiplatform">
<meta name="description" content="Snake in IronPython Mono Winforms">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Snake in IronPython Mono Winforms</h1>

<p>
In this part of the Mono IronPython Winforms programming tutorial, we 
will create a Snake game clone. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<h2>Snake game</h2>

<p>
<b>Snake</b> is an older classic video game. It was first created in late 70s. 
Later it was brought to PCs. In this game the player
controls a snake. The objective is to eat as many apples as possible. 
Each time the snake eats an apple, its body grows. The snake must avoid the 
walls and its own body. This game is sometimes called <b>Nibbles</b>. 
</p>


<h2>Development</h2>

<p>
The size of each of the joints of a snake is 10px. The snake is controlled with 
the cursor keys. Initially the snake has three joints. The game starts immediately. 
If the game is finished, we display Game Over message in the middle of the Board. 
</p>

<div class="codehead">board.py</div>
<pre class="code">
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System")


from System.Windows.Forms import UserControl, Keys, Timer
from System.Drawing import Size, Color, Bitmap, Brushes, RectangleF
from System.Drawing import Font, StringAlignment, StringFormat, PointF
from System import Random
from System.ComponentModel import Container

WIDTH = 300
HEIGHT = 300
DOT_SIZE = 10
ALL_DOTS = 900
RAND_POS = 27

x = [0] * ALL_DOTS
y = [0] * ALL_DOTS


class Board(UserControl):

    def __init__(self):
        self.Text = 'Snake'

        self.components = Container()
        self.BackColor = Color.Black
        self.DoubleBuffered = True
        self.ClientSize = Size(WIDTH, HEIGHT)

        self.left = False
        self.right = True
        self.up = False
        self.down = False
        self.inGame = True

        try: 
            self.dot = Bitmap("dot.png")
            self.apple = Bitmap("apple.png")
            self.head = Bitmap("head.png")

        except Exception, e:
            print e.Message
        

        self.initGame()


    def OnTick(self, sender, event):

        if self.inGame:
            self.checkApple()
            self.checkCollision()
            self.move()
        
        self.Refresh()
    
    def initGame(self):

        self.dots = 3

        for i in range(self.dots):
            x[i] = 50 - i * 10
            y[i] = 50
        

        self.locateApple()
        self.KeyUp += self.OnKeyUp


        self.timer = Timer(self.components)
        self.timer.Enabled = True
        self.timer.Interval = 100
        self.timer.Tick += self.OnTick

        self.Paint += self.OnPaint


    def OnPaint(self, event):

        g = event.Graphics
 
        if (self.inGame):
            g.DrawImage(self.apple, self.apple_x, self.apple_y)

            for i in range(self.dots):
                if i == 0:
                    g.DrawImage(self.head, x[i], y[i])
                else:
                    g.DrawImage(self.dot, x[i], y[i])     
               
        else:
           self.gameOver(g)
        
    

    def gameOver(self, g):

        msg = "Game Over"
        format = StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center

        width = float(self.ClientSize.Width)
        height = float(self.ClientSize.Height)
        rectf = RectangleF(0.0, 0.0, width, height)

        g.DrawString(msg, self.Font, Brushes.White, rectf, format)    
        self.timer.Stop()
    

    def checkApple(self):

        if x[0] == self.apple_x and y[0] == self.apple_y: 
            self.dots = self.dots + 1
            self.locateApple()
        
    
    def move(self):

        z = self.dots

        while z > 0:
            x[z] = x[(z - 1)]
            y[z] = y[(z - 1)]
            z = z - 1

        if self.left:
            x[0] -= DOT_SIZE

        if self.right: 
            x[0] += DOT_SIZE

        if self.up:
            y[0] -= DOT_SIZE

        if self.down:
            y[0] += DOT_SIZE
        
    

    def checkCollision(self):

        z = self.dots
       
        while z > 0:
            if z > 4 and x[0] == x[z] and y[0] == y[z]:
                self.inGame = False
            z = z - 1

        if y[0] >= HEIGHT - DOT_SIZE - self.TITLEBAR_HEIGHT:
            self.inGame = False
        
        if y[0] < 0:
            self.inGame = False
        
        if x[0] >= WIDTH - DOT_SIZE - self.BORDER_WIDTH:
            self.inGame = False

        if x[0] < 0:
            self.inGame = False
        

    def locateApple(self):
        rand = Random()
        r = rand.Next(RAND_POS)
        self.apple_x = r * DOT_SIZE
        r = rand.Next(RAND_POS)
        self.apple_y = r * DOT_SIZE
    

    def OnKeyUp(self, event): 

        key = event.KeyCode

        if key == Keys.Left and not self.right: 
            self.left = True
            self.up = False
            self.down = False
        

        if key == Keys.Right and not self.left:
            self.right = True
            self.up = False
            self.down = False
        

        if key == Keys.Up and not self.down:
            self.up = True
            self.right = False
            self.left = False
        

        if key == Keys.Down and not self.up: 
            self.down = True
            self.right = False
            self.left = False
</pre>


<p>
First we will define the constants used in our game. 
</p>


<p>
The <b class="keyword">WIDTH</b> and <b class="keyword">HEIGHT</b> constants 
determine the size of the Board. The <b class="keyword">DOT_SIZE</b> is the 
size of the apple and the dot of the snake. The <b class="keyword">ALL_DOTS</b> 
constant defines the maximum number of possible dots on the Board. (900 = 300*300/10*10)
The <b class="keyword">RAND_POS</b> constant is used to calculate a random 
position of an apple. The <b class="keyword">DELAY</b> constant determines 
the speed of the game.
</p>

<pre class="explanation">
 x = [0] * ALL_DOTS
 y = [0] * ALL_DOTS
</pre>

<p>
These two lists store x, y coordinates of all possible joints of a snake. 
</p>


<p>
In the <b class="keyword">move()</b> method we have the key algorithm of the 
game. To understand it, look at how the snake is moving. You control the head 
of the snake. You can change its direction with the cursor keys. The rest of the joints move
one position up the chain. The second joint moves where the first was, the 
third joint where the second was etc. 
</p>

<pre class="explanation">
 while z > 0:
     x[z] = x[(z - 1)]
     y[z] = y[(z - 1)]
     z = z - 1
</pre>

<p>
This code moves the joints up the chain.
</p>

<pre class="explanation">
 if self.left:
     x[0] -= DOT_SIZE
</pre>

<p>
Move the head to the left.
</p>

<p>
In the <b class="keyword">checkCollision()</b> method, we determine if the snake has 
hit itself or one of the walls. 
</p>

<pre class="explanation">
 while z > 0:
     if z > 4 and x[0] == x[z] and y[0] == y[z]:
         self.inGame = False
     z = z - 1
</pre>

<p>
Finish the game, if the snake hits one of its joints with the head. 
</p>

<pre class="explanation">
 if y[0] >= HEIGHT - DOT_SIZE - self.TITLEBAR_HEIGHT:
     self.inGame = False
</pre>

<p>
Finish the game, if the snake hits the bottom of the Board. 
</p>

<p>
The followintg image helps understand the collision of the snake object 
with the bottom of the board.
</p>

<br>
<img src="/img/gui/ironpython/board.png" alt="Collision">
<div class="figure">Figure: Collision</div>


<p>
The <b class="keyword">locateApple()</b> method locates an apple randomly 
on the form.
</p>

<pre class="explanation">
 rand = Random()
 r = rand.Next(RAND_POS)
</pre>

<p>
We get a random number from 0 to RAND_POS - 1.
</p>

<pre class="explanation">
 self.apple_x = r * DOT_SIZE
 ...
 self.apple_y = r * DOT_SIZE
</pre>

<p>
These line set the x, y coordinates of the apple
object. 
</p>

<p>
In the <b class="keyword">OnKeyUp()</b> method, we 
deternime which keys the player hit.
</p>

<pre class="explanation">
 if key == Keys.Left and not self.right: 
     self.left = True
     self.up = False
     self.down = False
</pre>

<p>
If we hit the left cursor key, we set <b class="keyword">self.left</b> variable to 
True. This variable is used in the <b class="keyword">move()</b>
method to change coordinates of the snake object. Notice also, that
when the snake is heading to the right, we cannot turn immediately 
to the left. 
</p>

<div class="codehead">snake.py</div>
<pre class="code">
#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")

from System.Windows.Forms import Application, Form, FormBorderStyle
from board import Board


class IForm(Form):

    def __init__(self):
        self.Text = 'Snake'
       
        self.FormBorderStyle = FormBorderStyle.FixedSingle
        
        borderWidth = (self.Width - self.ClientSize.Width) / 2
        titleBarHeight = self.Height - self.ClientSize.Height - borderWidth
        
        board = Board()
        board.BORDER_WIDTH = borderWidth
        board.TITLEBAR_HEIGHT = titleBarHeight

        self.Controls.Add(board)
        self.CenterToScreen()

Application.Run(IForm())
</pre>

<p>
This is the main class. 
</p>

<pre class="explanation">
 borderWidth = (self.Width - self.ClientSize.Width) / 2
 titleBarHeight = self.Height - self.ClientSize.Height - borderWidth
</pre>

<p>
Here we get the border width and the title bar height of the form control.
These values are necessary for the collision detection of the snake
with the borders. 
</p>

<pre class="explanation">
 board.BORDER_WIDTH = borderWidth
 board.TITLEBAR_HEIGHT = titleBarHeight
</pre>

<p>
We make them available for the board.
</p>


<br>
<img src="/img/gui/ironpython/snake.png" alt="Snake">
<div class="figure">Figure: Snake</div>

<hr class="btm">

<p>
This was the Snake game programmed using the Mono Winforms library for the IronPython programming
language.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified January 31, 2009  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>


